#!/usr/local/bin/perl -w

use App::Options (
    options => [qw(dbdriver dbclass dbhost dbname dbuser dbpass)],
    option => {
        dbclass  => { default => "App::Repository::MySQL", },
        dbdriver => { default => "mysql", },
        dbhost   => { default => "localhost", },
        dbname   => { default => "test", },
        dbuser   => { default => "", },
        dbpass   => { default => "", },
    },
);

use Test::More qw(no_plan);
use lib "../App-Context/lib";
use lib "../../App-Context/lib";
use lib "lib";
use lib "../lib";
use lib ".";
use lib "t";

use App;
use App::Repository;
use RepositoryTestUtils qw(create_table_test_person drop_table_test_person create_table drop_table);
use strict;

if (!$App::options{dbuser}) {
    ok(1, "No dbuser given. Tests assumed OK. (add dbuser=xxx and dbpass=yyy to app.conf in 't' directory)");
    exit(0);
}

my $context = App->context(
    conf_file => "",
    conf => {
        Repository => {
            default => {
                class => $App::options{dbclass},
                dbdriver => $App::options{dbdriver},
                dbhost => $App::options{dbhost},
                dbname => $App::options{dbname},
                dbuser => $App::options{dbuser},
                dbpass => $App::options{dbpass},
                table => {
                    test_person2 => {
                        primary_key => "last_name,first_name",
                    },
                    test_person3 => {
                        primary_key => [ "person_id" ],
                    },
                    test_person4 => {
                        primary_key => "person_id",
                    },
                },
            },
        },
    },
    debug_sql => $App::options{debug_sql},
    trace => $App::options{trace},
);

my $db = $context->repository();
&drop_table_test_person($db);
&create_table_test_person($db);
&drop_table($db,"test_country");
&create_table($db,"test_country");

$db->_load_rep_metadata();
###########################################################################
# METADATA TESTS
###########################################################################
my $table_names = $db->get_table_names();
#print "tables=", join(" ", @$table_names), "]\n";
my %tables = ( map { $_ => 1 } @$table_names );
#ok(defined $tables{test_person}, "get_table_names()");
$db->_load_table_metadata("test_person");
$db->_load_table_metadata("test_person2");
$db->_load_table_metadata("test_person3");
$db->_load_table_metadata("test_person4");
$db->_load_table_metadata("test_country");
is_deeply($db->{table}{test_person}{primary_key},  ["person_id"],  "primary_key (test_person) set from db");
is_deeply($db->{table}{test_person2}{primary_key}, ["last_name", "first_name"], "primary_key set from config (comma-sep)");
is_deeply($db->{table}{test_person3}{primary_key}, ["person_id"],  "primary_key set from config (scalar)");
is_deeply($db->{table}{test_person4}{primary_key}, ["person_id"],  "primary_key set from config (scalar)");
is_deeply($db->{table}{test_country}{primary_key}, ["country_id"], "primary_key (test_country) set from db");

my $tabledef = $db->get_table_def("test_person");
is($tabledef->{column}{person_id}{type},    "integer",  "person_id type is integer");
is($tabledef->{column}{first_name}{type},   "string",   "first_name type is string");
is($tabledef->{column}{country}{type},      "string",   "country type is string");
is($tabledef->{column}{gender}{type},       "string",   "gender type is string");
is($tabledef->{column}{birth_dt}{type},     "date",     "birth_dt type is date");
is($tabledef->{column}{age}{type},          "integer",  "age type is integer");
is($tabledef->{column}{chess_rating}{type}, "float",    "chess_rating type is float");
is($tabledef->{column}{modify_dttm}{type},  "datetime", "modify_dttm type is datetime");

is($tabledef->{column}{person_id}{quoted},    0, "person_id quoted is 0");
is($tabledef->{column}{first_name}{quoted},   1, "first_name quoted is 1");
is($tabledef->{column}{country}{quoted},      1, "country quoted is 1");
is($tabledef->{column}{gender}{quoted},       1, "gender quoted is 1");
is($tabledef->{column}{birth_dt}{quoted},     1, "birth_dt quoted is 1");
is($tabledef->{column}{age}{quoted},          0, "age quoted is 0");
is($tabledef->{column}{chess_rating}{quoted}, 0, "chess_rating quoted is 0");
is($tabledef->{column}{modify_dttm}{quoted},  1, "modify_dttm quoted is 1");

&drop_table($db,"test_person");
&drop_table($db,"test_country");

exit 0;

